# Maintainer: Norbert Pfeiler <norbert.pfeiler@gmail.com>
# Contributor: Andrew Sun <adsun701@gmail.com>

_realname=snappy
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1.10
pkgrel=1
pkgdesc="A fast compressor/decompressor library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('BSD')
url="https://github.com/google/snappy"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
checkdepends=("${MINGW_PACKAGE_PREFIX}-zlib"
              "${MINGW_PACKAGE_PREFIX}-lzo2")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/google/snappy/archive/${pkgver}.tar.gz"
        "0001-cmake-add-pkgconfig.patch"
        "0002-remove-Werror-flag.patch"
        "snappy.pc.in")
sha256sums=('49d831bffcc5f3d01482340fe5af59852ca2fe76c3e05df0e67203ebbe0f1d90'
            '2a204d7d35509ffc290d80c641fcb7e8488c67ca550aa1695493cff12c53156b'
            'a687b1a6e07e508bf43b0cce8059c1ddba74195b4b7b822643a0d06d3083c46d'
            '134f06ca0584a1026538d0fb972fc141c008390ecae1806184b721eca1abbc75')

prepare() {
  cd "${_realname}-${pkgver}"

  # upstream dropped the pkgconfig file and isn't interested in adding it back.
  # https://github.com/google/snappy/pull/55.patch
  cp -v "${srcdir}/snappy.pc.in" ./
  patch -p1 -i "${srcdir}/0001-cmake-add-pkgconfig.patch"

  patch -p1 -i "${srcdir}/0002-remove-Werror-flag.patch"
}

build() {
  # Shared Build
  [[ -d "${srcdir}"/build-${MSYSTEM}-shared ]] && rm -rf "${srcdir}"/build-${MSYSTEM}-shared
  mkdir -p ${srcdir}/build-${MSYSTEM}-shared && cd ${srcdir}/build-${MSYSTEM}-shared
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      -DBUILD_SHARED_LIBS=ON \
      -DSNAPPY_BUILD_TESTS=OFF \
      -DSNAPPY_BUILD_BENCHMARKS=OFF \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./

  # Static Build
  [[ -d "${srcdir}"/build-${MSYSTEM}-static ]] && rm -rf "${srcdir}"/build-${MSYSTEM}-static
  mkdir -p ${srcdir}/build-${MSYSTEM}-static && cd ${srcdir}/build-${MSYSTEM}-static
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      -DBUILD_SHARED_LIBS=OFF \
      -DSNAPPY_BUILD_TESTS=OFF \
      -DSNAPPY_BUILD_BENCHMARKS=OFF \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  # Static Install
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  # Shared Install
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .
}
